package com.hysteria.game;

import java.util.LinkedList;

import org.lwjgl.opengl.Display;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.KeyListener;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Shape;

import com.hyteria.render.Renderable;
import com.hyteria.render.Renderer;


public class Player extends Entity implements KeyListener, MouseListener{
	private static Player player;
	
	public static void create(GameContainer gc){
		player = new Player();
		gc.getInput().addKeyListener(player);
		gc.getInput().addMouseListener(player);
		
		Renderer.setRenderableDepth(player, 1);
		player.placeInCell(0, 0);
		player.setWidth(Cell.cellSize);
		player.setHeight(Cell.cellSize);
	}
	
	public static Player getPlayer(){
		return player;
	}
	
	private LinkedList<Action> actions = new LinkedList<Action>();
	
	public Player() {
		super(new Circle(10, 10, 10));
		setColor(Color.red);
	}

	@Override
	public void update() {
		if(!actions.isEmpty()){
			Game.progressTurn();
		}
	}
	
	@Override 
	public void onTurn(){
		Action a = actions.poll();
		if(a != null){
			a.perform();
		}
	}

	@Override
	public void stateChanged() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputEnded() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputStarted() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isAcceptingInput() {
		return true;
	}

	@Override
	public void setInput(Input arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(int button, int x, int y, int count) {
		if(count == 1){
			
			Cell c = Game.getMap().getCell(Game.getMap().getGridFromWorld(x), Game.getMap().getGridFromWorld(y));
			if(c == null) return;
			System.out.println(c);
		}else{
			System.out.println(this);
		}
	}

	@Override
	public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseWheelMoved(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(int key, char c) {
		
	}

	@Override
	public void keyReleased(int key, char c) {
		if(key == Input.KEY_W){ //Up
			moveTo(getCell().getGridX(),getCell().getGridY()-1);
		}else if(key == Input.KEY_A){ //Left
			moveTo(getCell().getGridX()-1,getCell().getGridY());
		}else if(key == Input.KEY_S){ //Down
			moveTo(getCell().getGridX(),getCell().getGridY()+1);
		}else if(key == Input.KEY_D){ //Right
			moveTo(getCell().getGridX()+1,getCell().getGridY());
		}
	}
	
	public void moveTo(final int gX, final int gY){
		if(!canMoveTo(gX, gY)) return;
		
		
		Action move = new Action(){
			@Override public void perform(){
				Entity e = (Entity)target;
				e.placeInCell(gX, gY);
			}
		};
		move.type = Action.Type.Move;
		move.target = this;
		
		actions.add(move);
	}
	
	public boolean canMoveTo(int gX, int gY){
		Cell c = Game.getMap().getCell(gX, gY);
		if(c != null){
			if(!c.isCollidable())
				return true;
		}
		
		return false;
	}
	
	public String toString(){
		String s = new String();
		s += "Player action queue: " + actions.toString() + "\n";
		return s;
	}
}
